package com.guomei.controller.work;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guomei.bean.work.domain.Work;
import com.guomei.bean.work.vo.SelectWorkDTO;
import com.guomei.bean.work.vo.WorkVo;
import com.guomei.mapper.UserMapper;
import com.guomei.mapper.work.WorkMapper;
import com.guomei.utils.PageResult;
import com.guomei.utils.Result;

@RestController
@RequestMapping("/api1/work")
public class WorkController {

    @Resource
    private WorkMapper workMapper;

    @Resource
    private UserMapper userMapper;

    @PostMapping("/select")
    public Result<?> selectWork(@RequestBody SelectWorkDTO selectWorkDTO) {

        int pageSize = selectWorkDTO.getPageSize() == null ? 10 : selectWorkDTO.getPageSize();
        int pageNum = selectWorkDTO.getPageNum() == null ? 1 : selectWorkDTO.getPageNum();

        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();
        if (selectWorkDTO.getStatus() != null) {
            queryWrapper.eq("status", selectWorkDTO.getStatus());
        }
        if (selectWorkDTO.getName() != null) {
            queryWrapper.like("name", selectWorkDTO.getName());
        }
        if (selectWorkDTO.getTag() != null) {
            queryWrapper.like("tag", selectWorkDTO.getTag());
        }
        if (selectWorkDTO.getIsWinner() != null) {
            queryWrapper.eq("is_winner", selectWorkDTO.getIsWinner());
        }
        if (selectWorkDTO.getIds() != null) {
            queryWrapper.in("id", selectWorkDTO.getIds());
        }
        if (selectWorkDTO.getUserId() != null) {
            queryWrapper.eq("user_id", selectWorkDTO.getUserId());
        }
        queryWrapper.last("limit " + pageSize + " offset " + (pageNum - 1) * pageSize);

        List<Work> works = workMapper.selectList(queryWrapper);

        QueryWrapper<Work> countWrapper = new QueryWrapper<>();
        if (selectWorkDTO.getStatus() != null) {
            countWrapper.eq("status", selectWorkDTO.getStatus());
        }
        if (selectWorkDTO.getName() != null) {
            countWrapper.like("name", selectWorkDTO.getName());
        }
        if (selectWorkDTO.getTag() != null) {
            countWrapper.like("tag", selectWorkDTO.getTag());
        }
        if (selectWorkDTO.getUserId() != null) {
            countWrapper.eq("user_id", selectWorkDTO.getUserId());
        }
        if (selectWorkDTO.getIsWinner() != null) {
            countWrapper.eq("is_winner", selectWorkDTO.getIsWinner());
        }
        if (selectWorkDTO.getIds() != null) {
            countWrapper.in("id", selectWorkDTO.getIds());
        }
        Long total = workMapper.selectCount(countWrapper);

        List<WorkVo> workVos = works.stream().map(work -> {
            WorkVo workVo = new WorkVo();
            workVo.setId(work.getId());
            workVo.setName(work.getName());
            workVo.setTag(work.getTag());
            workVo.setCover(work.getCover());
            workVo.setVideo(work.getVideo());
            workVo.setUserId(work.getUserId());
            workVo.setContent(work.getContent());
            workVo.setCreatedTime(work.getCreatedTime());
            workVo.setUpdatedTime(work.getUpdatedTime());
            workVo.setViewCount(work.getViewCount());
            workVo.setLikeCount(work.getLikeCount());
            workVo.setCommentCount(work.getCommentCount());
            workVo.setUser(userMapper.selectById(work.getUserId()));
            workVo.setDescription(work.getDescription());
            workVo.setIsWinner(work.isWinner());
            return workVo;
        }).collect(Collectors.toList());

        PageResult<List<WorkVo>> pageResult = PageResult.success(workVos, total, pageSize, pageNum);
        return Result.success(pageResult);
    }

    @PostMapping("/save")
    public Result<?> saveWork(@RequestBody Work work) {
        if (work.getId() == null) {
            workMapper.insert(work);
        } else {
            workMapper.updateById(work);
        }
        return Result.success(workMapper.selectById(work.getId()));
    }


    @DeleteMapping("/deleteByIds")
    public Result<?> deleteWork(@RequestBody List<Long> ids) {
        workMapper.deleteBatchIds(ids);
        return Result.success(ids);
    }
    

    @PostMapping("/like")
    public Result<?> likeWork(@RequestBody Work work) {
        work = workMapper.selectById(work.getId());
        work.setLikeCount(work.getLikeCount() + 1);
        workMapper.updateById(work);
        return Result.success(work);
    }

    @PostMapping("/view")
    public Result<?> viewWork(@RequestBody Work work) {
        work = workMapper.selectById(work.getId());
        work.setViewCount(work.getViewCount() + 1);
        workMapper.updateById(work);
        return Result.success(work);
    }
}
